/*
 * Copyright 2008 Andrej Herich
 * 
 * http://azetclient.yw.sk/
 * http://azetclient.googlecode.com/
 * 
 * This file is part of AzetClient.
 * 
 * AzetClient is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * AzetClient is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with AzetClient.  If not, see <http://www.gnu.org/licenses/>.
 */

package sk.yw.azetclient.connection;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import org.apache.log4j.Logger;
import sk.yw.azetclient.Main;

/**
 *
 * @author error216
 */
public class PagesSaver {
    
    private static final Logger logger = Logger.getLogger(PagesSaver.class);
    
    private static final String PAGE_EXTENSION = ".html";
    
    private static boolean savePages = 
            "true".equals(Main.getProperty("azetclient.savePages"));
    private static String savePagesDirectory =
            Main.CONFIG_DIR + File.separator
            + Main.getProperty("azetclient.savePages.directory");
    static {
        if (savePages) {
            File dir = new File(savePagesDirectory);
            if (!dir.exists()) {
                if (!dir.mkdirs()) {
                    logger.error("Unable to create save pages directory.");
                }
            }
        }
    }
    
    private static Map<String,Integer> idCounts =
            new HashMap<String,Integer>();
    
    private String id;
    private PrintWriter writer;
    
    public PagesSaver(String uniqId) {
        if (uniqId == null) {
            throw new IllegalArgumentException("Null pointer in uniqId");
        }
        int count = 0;
        if (idCounts.containsKey(uniqId)) {
            count = idCounts.get(uniqId) + 1;
        }
        idCounts.put(uniqId, count);
        
        boolean save = savePages;
        String localSavePages =
                Main.getProperty("azetclient." + uniqId + ".savePages");
        if (localSavePages != null) {
            save = "true".equals(localSavePages);
        }
        
        id = uniqId;
        if (save) {
            try {
                writer = new PrintWriter(savePagesDirectory + File.separator
                        + id + count + PAGE_EXTENSION);
            } catch (FileNotFoundException ex) {
                logger.warn("Unable to create PrintWriter for " + uniqId, ex);
            }
        }
    }
    
    public void println() {
        if (writer != null) writer.println();
    }
    
    public void println(String line) {
        if (writer != null) writer.println(line);
    }
    
    public void close() {
        if (writer != null) writer.close();
    }
}
